package com.sample.ab;

import java.io.*;
import java.util.*;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author 张智研
 * @date 2022/10/12 15:29
 **/
public class CsvFile {

    public static Map<String, JSONObject> map = new LinkedHashMap<>();

    List<String[]> ReadMethod() {
        String csvFile = "/Users/zhangzhiyan/Desktop/data.txt";
        String line = "";
        String cvsSplitBy = "\t";
        int count = 0;
        String[] Line = new String[1];
        List<String[]> BikeDataList = new ArrayList<String[]>();

        try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
            while ((line = br.readLine()) != null) {
                if(count++ == 0) {
                    System.out.println(1);
                    continue;
                }
                Line = line.split(cvsSplitBy);//将逗号作为分隔符 划分一行 得到数组[]
                BikeDataList.add(Line);
                float a = Float.parseFloat(Line[4].replace(",", "").replace("\"", ""));
                System.out.println("行号:"+(count++)+" id:"+Line[0]+" 产品: "+Line[1]+ "  pid:"+Line[2]+ "  物料:"+Line[3]+ "  金额:"+ a);
                map.computeIfAbsent(Line[0], k -> new JSONObject());
                map.get(Line[0]).put("id",Line[0]);
                map.get(Line[0]).put("cp",Line[1]);
                map.get(Line[0]).put("pid",Line[2]);
                map.get(Line[0]).put("wl",Line[3]);
                map.get(Line[0]).put("je",a);
                map.get(Line[0]).put("je1",0F);
                map.get(Line[0]).put("je2",0F);
            }
            System.out.println(map.size());
            this.sum("1");



            File file = new File("/Users/zhangzhiyan/Desktop/result.csv");
            //构建输出流，同时指定编码
            OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(file));
            //csv文件是逗号分隔，除第一个外，每次写入一个单元格数据后需要输入逗号
            ow.write("INDEX");
            ow.write(",");
            ow.write("CPSEQ");
            ow.write(",");
            ow.write("PARENT");
            ow.write(",");
            ow.write("MATNR");
            ow.write(",");
            ow.write("KST018");
            ow.write(",");
            ow.write("KST018_JG");
            ow.write(",");
            ow.write("KST018_ZP");
            //写完文件头后换行
            ow.write("\r\n");
            //写内容
            map.values().forEach((jsonObject) -> {
                try {
                    ow.write(jsonObject.getString("id"));
                    ow.write(",");
                    ow.write(jsonObject.getString("cp"));
                    ow.write(",");
                    ow.write(jsonObject.getString("pid"));
                    ow.write(",");
                    ow.write(jsonObject.getString("wl"));
                    ow.write(",");
                    ow.write(jsonObject.getString("je"));
                    ow.write(",");
                    ow.write(jsonObject.getString("je1"));
                    ow.write(",");
                    ow.write(jsonObject.getString("je2"));
                    ow.write(",");
                    //写完一行换行
                    ow.write("\r\n");
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            });
            ow.flush();
            ow.close();
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
        return BikeDataList;
    }

    public Map<String, Double> sum(String id) throws JSONException {
        JSONObject jsonObject = map.get(id);
        double je = jsonObject.getDouble("je");
        double je1 = jsonObject.getDouble("je1");
        double je2 = jsonObject.getDouble("je2");
        String pid = jsonObject.getString("pid");
        if(map.get(pid) != null) {
            Map<String, Double> result = this.sum(pid);
            je1 = je1 + result.get("je1");
            je2 = je2 + result.get("je2");
        }else {
            String wl = jsonObject.getString("wl");
            if(wl.startsWith("13")) {
                je1 = je + je1;
            }else {
                je2 = je + je2;
            }
        }
        jsonObject.put("je1",je1);
        jsonObject.put("je2",je2);
        Map<String, Double> result = new HashMap<>();
        result.put("je1",je1);
        result.put("je2",je2);
        return result;
    }

    public static void main(String[] args) {
        new CsvFile().ReadMethod();
    }
}
